//
//  HotSaleTableViewCell.m
//  Beauty
//
//  Created by Snow on 16/9/12.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "HotSaleTableViewCell.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "HotSaleCollectionViewCell.h"
#import "ModelOfHotSale.h"
@interface HotSaleTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@end
@implementation HotSaleTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.minimumLineSpacing = 10;
        _layout.minimumInteritemSpacing = 10;
        _layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView.mas_top);
            make.left.mas_equalTo(self.contentView.mas_left);
            make.right.mas_equalTo(self.contentView.mas_right);
            make.bottom.mas_equalTo(self.contentView.mas_bottom);
        }];
        _layout.scrollDirection = UICollectionViewScrollPositionNone;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[HotSaleCollectionViewCell class] forCellWithReuseIdentifier:@"HotSaleCollectionViewCell"];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.layout.itemSize = CGSizeMake((self.contentView.bounds.size.width - 30) / 2, 280);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayOfModel.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotSaleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotSaleCollectionViewCell" forIndexPath:indexPath];
    ModelOfHotSale *model = [_arrayOfModel objectAtIndex:indexPath.row];
    [cell.imageOfGoods sd_setImageWithURL:[NSURL URLWithString:model.goods_image_url]];
    if (model.goods_name.length < 8) {
        cell.labelOfName.text = model.goods_name;
    } else {
        cell.labelOfName.text = [model.goods_name substringToIndex:8];
    }
    cell.labelOfPrice.text = [NSString stringWithFormat:@"￥ %@" , model.goods_price];
    return cell;
}
- (void)setArrayOfModel:(NSArray *)arrayOfModel
{
    _arrayOfModel = arrayOfModel;
    [_collectionView reloadData];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(hotScaleWithGood_ID:)]) {
        ModelOfHotSale *model = [_arrayOfModel objectAtIndex:indexPath.row];
        [self.delegate hotScaleWithGood_ID:model.goods_id];
    }
}


@end
